package LeetCode.longestUnivaluePath;

/**
 * @author Chen Hongyu
 */

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode(int x) { val = x; }
 * }
 */
class Solution {

    int ans = 0;

    public int longestUnivaluePath(TreeNode root) {
        if (root == null) {
            return 0;
        }
        ansLength(root);
        return ans;
    }

    public int ansLength(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = ansLength(node.left);
        int right = ansLength(node.right);
        int arrowLeft = 0;
        int arrowRight = 0;
        if (node.left != null && node.left.val == node.val) {
            arrowLeft += left + 1;
        }
        if (node.right != null && node.right.val == node.val) {
            arrowRight += right + 1;
        }
        ans = Math.max(ans, arrowLeft + arrowRight);
        return Math.max(arrowLeft, arrowRight);
    }


}
